<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/27
 * Time: 11:56
 */

require("Include/conn.php");

if(isset($_COOKIE['user_id'])&&is_numeric($_COOKIE['user_id'])){
    $user_id = $_COOKIE['user_id'];
    $pic_id = $_GET["pic_id"];

    $sql =  "SELECT * FROM `pw_fav` WHERE `fav_picid`=$pic_id AND `fav_userid`=$user_id";
    $check = mysqli_query($link,$sql);
    $exist = mysqli_fetch_row($check);


    if(!$exist){
        $sql = "SELECT `pic_favcnt` FROM `pw_pic` WHERE `pic_id`=$pic_id;";
        $result = mysqli_query($link,$sql);
        $favcntarray = mysqli_fetch_row($result);
        $favcnt = $favcntarray[0];
        echo $favcnt;
        $favcnt = $favcnt+1;
        $sql = "UPDATE `pw_pic` SET `pic_favcnt`=$favcnt WHERE `pic_id`=$pic_id;";
        $result = mysqli_query($link,$sql);

        $sql = "INSERT INTO `pw_fav` (`fav_picid`,`fav_userid`) VALUES ($pic_id,$user_id)";
        $result = mysqli_query($link,$sql);
    }
    else{
        echo "您已经收藏过这张图片";
    }

    header("location:http://localhost/mark/index.php");
}
else{
    header('location: http://localhost/mark/login.php');
}


?>